/**
 * 
 */
package npap.cater.unr.edu.client.scaffold.place.customized;

import npap.cater.unr.edu.client.scaffold.place.ProxyListPlace;
import npap.cater.unr.edu.client.scaffold.place.ProxyPlace;
import npap.cater.unr.edu.client.scaffold.place.ProxyPlaceToListPlace;

import com.google.gwt.activity.shared.FilteredActivityMapper;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;

/**
 * @author Hao
 *
 */
public class AppPlaceToProjectListPlace implements FilteredActivityMapper.Filter {
	/**
	 * @uml.property  name="placeController"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private PlaceController placeController;

	public AppPlaceToProjectListPlace(PlaceController placeController) {
		super();
		this.placeController = placeController;
	}

	@Override
	public Place filter(Place place) {
		// TODO Auto-generated method stub
		return projectListPlaceFor(place);
	}

	/**
	 * Required by {@link FilteredActivityMapper.Filter}, calls
	 * {@link #proxyListPlaceFor()}.
	 */

	/**
	 * @param place
	 *            a place to process
	 * @return an appropriate ProxyListPlace, or null if the given place has
	 *         nothing to do with proxies
	 */
	public Place projectListPlaceFor(Place place) {
		if (place instanceof ProjectListPlace) {
			return (ProjectListPlace) place;
		}

		if (place instanceof AppPlace) {
			AppPlace appPlace = (AppPlace) place;
//			ProxyPlaceToListPlace proxyPlaceToListPlace = new ProxyPlaceToListPlace();
//			return new ProjectListPlace(appPlace.getProjectID(),
//					appPlace.getProjectName());
			return appPlace;
		}

		if (place instanceof ProxyListPlace) {

			return (ProxyListPlace) place;
		}

		if (place instanceof ProxyPlace) {
			ProxyPlace proxyPlace = (ProxyPlace) place;
			if (placeController.getWhere() instanceof ProjectListPlace) {
				ProjectListPlace currentPlace = (ProjectListPlace) placeController
						.getWhere();
				return new ProjectListPlace(currentPlace.getIdProject(),
						currentPlace.getProjectName());
			}
			 return new ProxyListPlace(proxyPlace.getProxyClass());

		}
		return null;

		// ProjectListPlace projectPlace = (ProjectList) place;
		// return new ProjectListPlace(proxyPlace.getProxyClass());
	}
}

